home *** CD-ROM | disk | FTP | other *** search
/ QuickTime 2.0 Developer Kit / QuickTime 2.0 Developer Kit.iso / mac / MAC / Programming Stuff / Sample Code / Image Compression Mgr / Decompression & Scaling / Compression & Scaling.c next >
Encoding:
C/C++ Source or Header  |  1994-12-06  |  4.0 KB  |  179 lines  |  [TEXT/MPS ]

  1. /*
  2.   File:            Compression & Scaling.c
  3.   Contains:        Compression & Scaling Example
  4.   Written by:    Edgar Lee / DTS
  5.   Copyright:    © 1991-1994 by Apple Computer, Inc., all rights reserved.
  6.   Change History (most recent first):
  7.   <2>         12/4/94        khs        changed the format of the file to the new look and feel
  8.   <1>         10/20/92    el        1.0 Completed
  9.   To Do:
  10. */
  11.  
  12.  
  13. // INCLUDES
  14. #include    <GestaltEqu.h>
  15. #include    <ToolUtils.h>
  16. #include    <SegLoad.h>
  17. #include    <Fonts.h>
  18.  
  19. #include     <ImageCompression.h>
  20. #include     <Movies.h>
  21.  
  22. /* Constant Declarations */
  23.  
  24. #define WLEFT( x )    (((qd.screenBits.bounds.right - qd.screenBits.bounds.left) - x) / 2)
  25. #define WTOP( x )    (((qd.screenBits.bounds.bottom - qd.screenBits.bounds.top) - x) / 2)
  26. #define PICTID 128
  27.  
  28. // GLOBALS
  29. WindowPtr gWindow;
  30.  
  31.  
  32. // FUNCTION PROTOTYPES
  33. void InitMac();
  34. void CheckForQuickTime();
  35. void DoScaleTest();
  36. void CreateWindow();
  37.  
  38.  
  39. // MAIN
  40. void main(void)
  41. {
  42.     InitMac();
  43.     CheckForQuickTime();
  44.  
  45.     CreateWindow();
  46.     DoScaleTest();
  47.  
  48.     while (!Button())
  49.         ;
  50. }
  51.  
  52.  
  53. void InitMac()
  54. {
  55.     InitGraf(&qd.thePort);
  56.     InitFonts();
  57.     InitWindows();
  58.     InitMenus();
  59.     TEInit();
  60.     InitDialogs(nil);
  61.     InitCursor();
  62.     FlushEvents(0, everyEvent);
  63. }
  64.  
  65.  
  66. void CheckForQuickTime()
  67. {
  68.     long version;
  69.  
  70.     if (Gestalt(gestaltQuickTime, &version) != noErr)
  71.     {
  72.         ParamText("\pQuickTime not installed.  Please install, then try again.", "\p", "\p", "\p");
  73.         Alert(128, nil);
  74.         ExitToShell();
  75.     }
  76. }
  77.  
  78.  
  79. void CreateWindow()
  80. {
  81.     Rect bounds =
  82.     {
  83.         0,  0, 0, 0
  84.     };
  85.  
  86.     gWindow = NewCWindow(0L, &bounds, "\pDecompression & Scaling", 
  87.                             false, documentProc, (WindowPtr) - 1L, false, 0L);
  88.  
  89.     SetPort(gWindow);
  90. }
  91.  
  92.  
  93. void DoScaleTest()
  94. {
  95.     int i;
  96.     GWorldPtr srcGWorld;
  97.     PixMapHandle srcPixMap;
  98.     PicHandle pict;
  99.     Rect pictBounds;
  100.     Rect finalBounds;
  101.     CGrafPtr savedPort;
  102.     GDHandle savedDevice;
  103.     short scaleRatio = 4;
  104.  
  105.     ImageDescriptionHandle desc;
  106.     Ptr imageData;
  107.     long size;
  108.  
  109.     // Load the picture and define the source and dest rects.
  110.     pict = GetPicture(PICTID);
  111.  
  112.     pictBounds = (**pict).picFrame;
  113.     OffsetRect(&pictBounds, -pictBounds.left, -pictBounds.top);
  114.  
  115.     finalBounds = pictBounds;
  116.     finalBounds.right = finalBounds.right / scaleRatio;
  117.     finalBounds.bottom = finalBounds.bottom / scaleRatio;
  118.  
  119.     SizeWindow(gWindow, pictBounds.right + finalBounds.right, pictBounds.bottom, false);
  120.     MoveWindow(gWindow, WLEFT(pictBounds.right + finalBounds.right), 
  121.                         WTOP(pictBounds.bottom), false);
  122.     ShowWindow(gWindow);
  123.  
  124.     /********************************************************/
  125.     /* Create a temporary offscreen used to store the pict. */
  126.     /********************************************************/
  127.  
  128.     if (NewGWorld(&srcGWorld, 8, &pictBounds, GetCTable(8), nil, 0) != noErr)
  129.         ExitToShell();
  130.  
  131.     srcPixMap = GetGWorldPixMap(srcGWorld);
  132.     LockPixels(srcPixMap);
  133.  
  134.     /********************************************************/
  135.     /* Draw the picture into the temporary gworld & window. */
  136.     /********************************************************/
  137.  
  138.     GetGWorld(&savedPort, &savedDevice);
  139.     SetGWorld(srcGWorld, nil);
  140.     DrawPicture(pict, &pictBounds);
  141.  
  142.     SetGWorld(savedPort, savedDevice);
  143.     DrawPicture(pict, &pictBounds);
  144.  
  145.     /*************************************************/
  146.     /* Now, compress the picture into a data buffer, */
  147.     /*   then dispose the temporary gworld.          */
  148.     /*************************************************/
  149.  
  150.     if (GetMaxCompressionSize(srcPixMap, &(**srcPixMap).bounds, 8, codecMaxQuality, 
  151.                                 'raw ', bestSpeedCodec, &size))
  152.         ExitToShell();
  153.  
  154.     imageData = NewPtr(size);
  155.     desc = (ImageDescriptionHandle)NewHandle((Size)1);
  156.  
  157.     if (CompressImage(srcPixMap, &(**srcPixMap).bounds, codecMaxQuality, 
  158.                         'raw ', desc, imageData))
  159.         ExitToShell();
  160.  
  161.     DisposeGWorld(srcGWorld);
  162.  
  163.     /**********************************************************/
  164.     /* Decompress the data buffer to the window with scaling. */
  165.     /**********************************************************/
  166.  
  167.     OffsetRect(&finalBounds, pictBounds.right, 0);
  168.  
  169.     for (i = 0; i < scaleRatio; i++)
  170.     {
  171.         DecompressImage(imageData, desc, (*(CWindowPtr)gWindow).portPixMap, 
  172.                             &pictBounds, &finalBounds, srcCopy + ditherCopy, nil);
  173.  
  174.         OffsetRect(&finalBounds, 0, finalBounds.bottom - finalBounds.top);
  175.     }
  176. }
  177.  
  178.  
  179.